package queue_stack.leetcode.medium;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author bruin_du
 * @description 每日温度
 * @date 2022/8/13 21:36
 **/
public class OfferII038_dailyTemperatures {
    public int[] dailyTemperatures(int[] temperatures) {
        Deque<Integer> stack = new ArrayDeque<>();
        int len = temperatures.length;
        int[] arr = new int[len];
        for (int i = 0; i < len; i++) {
            if (stack.isEmpty() || temperatures[i] <= temperatures[stack.peek()]) {
                stack.push(i);
            } else {
                while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                    int index = stack.pop();
                    arr[index] = i - index;
                }
                stack.push(i);
            }
        }
        return arr;
    }
}
